package com.lc.YYGH.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.lc.YYGH.cmnclient.cmnclient.DictFeignClient;
import com.lc.YYGH.hosp.repository.LawerRepository;
import com.lc.YYGH.hosp.repository.OfficeRepository;
import com.lc.YYGH.hosp.service.OfficeService;
import com.lc.YYGH.model.hosp.BookingRule;
import com.lc.YYGH.model.hosp.Hospital;
import com.lc.YYGH.model.office.LawOffice;
import com.lc.YYGH.vo.hosp.HospitalQueryVo;
import com.lc.YYGH.vo.office.LawOfficeQueryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;



import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@Service
public class OfficeServiceImpl implements OfficeService {
    @Autowired
    private OfficeRepository officeRepository;
    @Autowired
    private DictFeignClient dictFeignClient;

    @Override
    public void save(Map<String, Object> paramMap) {
        //根据编号查询
        LawOffice lawOffice=JSONObject.parseObject(JSONObject.toJSONString(paramMap),LawOffice.class);
        LawOffice targetOffice= officeRepository.findByOfficecode(lawOffice.getOfficecode());
        if(null==targetOffice)
        {
            //数据库中不存在
           // hospital.setStatus(0);
            Date date=new Date();
            lawOffice.setCreateTime(date);
            lawOffice.setUpdateTime(date);
            officeRepository.save(lawOffice);
            System.out.println("test----");
        }
        else
        {
            targetOffice.setUpdateTime(new Date());
            targetOffice.setIsDeleted(0);
            //数据库中已存在，更新数据
            officeRepository.save(targetOffice);

        }

    }

    @Override
    public LawOffice findOffice(String officecode) {
       LawOffice lawOffice= officeRepository.findByOfficecode(officecode);
        return lawOffice;

    }

    @Override
    public Page<LawOffice> selectPage(Integer page, Integer limit, LawOfficeQueryVo lawOfficeQueryVo) {
        Sort sort=Sort.by(Sort.Direction.DESC,"createTime");
        Pageable pageable= PageRequest.of(page-1,limit,sort);
        //Hospital hospital=new Hospital();
        LawOffice lawOffice=new LawOffice();
        BeanUtils.copyProperties(lawOfficeQueryVo,lawOffice);
        ExampleMatcher macher=ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        Example<LawOffice> example=Example.of(lawOffice,macher);
        Page<LawOffice> pages=officeRepository.findAll(example,pageable);
        for(LawOffice lawOffice1:pages)
        {
            this.packLawOffice(lawOffice1);

        }
        return pages;
    }

    @Override
    public Map<String, Object> getDetail(String id) {
        LawOffice lawOffice=officeRepository.findById(id).get();
//        BookingRule bookingRule=hospital.getBookingRule();
        //创建map存储数据
        Map<String,Object> map=new HashMap<>();
        map.put("laweroffice",this.packLawOffice(lawOffice));
      //  map.put("bookingRule",bookingRule);

        //不需要重复返回
       // hospital.setBookingRule(null);
        return map;
    }

    private LawOffice packLawOffice(LawOffice lawOffice)
    {
        //调用Dict
        String proname= dictFeignClient.getName(lawOffice.getProvinceCode());
        String cityname= dictFeignClient.getName(lawOffice.getCityCode());
        String dicstrictname= dictFeignClient.getName(lawOffice.getDistrictCode());
        //获取等级名称


        lawOffice.getParam().put("fullAddress",proname+cityname+dicstrictname);
        return lawOffice;
    }
}
